﻿layui.use(['element', 'form', 'layer', 'table', 'laytpl', 'web'], function () {
    var element = layui.element;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var laytpl = layui.laytpl;
    var web = layui.web;
    var $ = layui.jquery;
    var table_info = "table_info";

    $("input[name='basePackageName']").val("com.sesame");
    $("input[name='author']").val("admin");

    //加载左边'数据库中所有的表'
    init_left_tables("");
    //键盘的监听事件
    $("#searchTableText").keyup(function () {
        if (event.keyCode == 13) {
            init_left_tables($("#searchTableText").val());
        }
    });
    //点击左边的表
    form.on('checkbox(table_checkbox)', function (data) {
        var value = data.value;
        if (data.elem.checked) {
            // 判断界面上有没有这个标签
            if (($("#tr_" + value).html() == null || $("#tr_" + value).html() == undefined)) {
                var json = getTableInfo(value); //获取表信息
                console.log(json);
                var getTpl = tr_id.innerHTML
                    , view = document.getElementById('table_body');
                laytpl(getTpl).render(json, function (html) {
                    view.innerHTML += html;
                    element.init();
                    form.render();

                });
            }
        } else {
            //删除一列
            $("#tr_" + value).remove();
        }

    });
    $(document).on("click", ".delete_img", function () {
        $("#tr_" + $(this).attr("value")).remove();
    });

    // 提交数据
    $("#sub").click(function () {
        check_submit(function () {
            document.basicForm.submit();
        });
    });
    // 预览数据
    $("#yulan").click(function () {
        check_submit(function () {
            var url = basePath + '/db/preview';
            var data = $("#basicForm").serialize();
            var type = $("#basicForm").attr("method");
            web.ajax(url, type, data, function () {
                //弹出预览层
                var indes = layer.open({
                    title: ['预览代码', 'font-size:18px;'],
                    type: 2,
                    //fixed : false,
                    shadeClose: true,
                    area: ['90%', '90%'], //宽,高
                    //offset : [ '100px', '100px' ],// top,left
                    content: basePath + '/page/preview',
                    end: function () {//层销毁时删除文件
                        $.post(basePath + "/db/deletedir.do", function (data) {
                        });
                    }
                });
            });
        });
    });

    //点击弹出层
    $(document).on("click", ".tableDetail", function () {
        showDetail(this.name, $(this).next().val());
    });

//---------------------------------------------------------------------
    /**
     * 检查并提交数据
     */
    function check_submit(successfu) {
        $(".tableName").each(function (i) {
            $(this).attr("name", "tables[" + i + "].tableName");
        });
        $(".className").each(function (i) {
            $(this).attr("name", "tables[" + i + "].className");
        });
        $(".packageName").each(function (i) {
            $(this).attr("name", "tables[" + i + "].packageName");
        });
        $(".dirPath").each(function (i) {
            $(this).attr("name", "tables[" + i + "].dirPath");
        });
        $(".page").each(function (i) {
            $(this).attr("name", "tables[" + i + "].page");
        });
        $(".add").each(function (i) {
            $(this).attr("name", "tables[" + i + "].add");
        });
        $(".delete").each(function (i) {
            $(this).attr("name", "tables[" + i + "].delete");
        });
        $(".update").each(function (i) {
            $(this).attr("name", "tables[" + i + "].update");
        });
        $(".selectList").each(function (i) {
            $(this).attr("name", "tables[" + i + "].selectList");
        });
        $(".selectPage").each(function (i) {
            $(this).attr("name", "tables[" + i + "].selectPage");
        });
        $(".comment").each(function (i) {
            $(this).attr("name", "tables[" + i + "].comment");
        });
        var count = 0;
        $(".select").each(function (i) {
            $(this).attr("name", "tables[" + i + "].select");
            count++;
        });
        if (count == 0) {
            layer.alert("请至少选择一个表", {
                icon: 2
            });
        } else {
            successfu();
        }
    }

    /**
     * 加载左边'数据库中所有的表'
     * @param tableName 表名
     */
    function init_left_tables(tableName) {
        web.ajax(basePath + "/db/searchTable", 'post', {name: tableName}, function (data) {
            var json = data.result;
            if (tableName == "") {
                layui.sessionData(table_info, {key: 'tables', value: json});
            }
            var getTpl = left_tables_tpl.innerHTML
                , view = document.getElementById('left_tables');
            laytpl(getTpl).render(json, function (html) {
                view.innerHTML = html;
                element.init();
                form.render();
            });
        });
    }

    /**
     * 获取表信息
     */
    function getTableInfo(tableName) {
        var json;
        var arr = layui.sessionData(table_info).tables;
        for (var i = 0; i < arr.length; i++) {
            if (tableName == arr[i].tableName) {
                json = arr[i];
                break;
            }
        }
        return json;
    }
});

//---------------------------------------------------------------------------
//  弹出层上面的 js
//---------------------------------------------------------------------------
//模块框里的 三个页面的事件 showDetail(tableName)
var tableMap = {};
//点击上面的分页按钮
$(document).on("click", ".li", function () {
    $(".liInfo").hide();
    $("#" + $(this).next().val()).show();
    $(".li").removeClass("active");
    $(this).addClass("active");
});

//点击详情后 显示小窗口
function showDetail(tableName, comment) {
    $("#table_as_name").val('');
    //显示加载图片
    $("#fieldInfo").html('<img  src="' + basePath + '/istyle/images/load.gif" /><img  src="' + basePath + '/istyle/images/load.gif" /><img  src="' + basePath + '/istyle/images/load.gif" /><img  src="' + basePath + '/istyle/images/load.gif" /><br/><h3>正在努力的加载中.....</h3>');

    $("#lableTableInfo").html(comment + ":" + tableName);
    $("#fieldInfo").show();
    $("#sqlInfo").hide();
    $("#docInfo").hide();
    if (tableName == '') {
        return;
    }
    $.post(basePath + '/db/searchTableDetail.do', {
        tableName: tableName
    }, function (list) {
        var all = '<p><input type="checkbox" id="fieldAll"/>&nbsp;&nbsp;全选</p>';
        for (var i = 0; i < list.length; i++) {
            all += '<p class="p' + list[i].name + '"><input type="checkbox"  class="fieldList" name="' + list[i].name + '" />&nbsp;&nbsp;' + list[i].name + '&nbsp;&nbsp;<b>' + list[i].type + '</b>&nbsp;&nbsp;//&nbsp;' + list[i].comment + '</p>';
            // 加载list 到tableMap中
            tableMap[list[i].name] = list[i];
        }
        $("#fieldInfo").html(all);
        //另外两个界面的数据 清空
        $("#liInfo_div1").html("");
        $("#liInfo_div2").html("");
        $("#liInfo_div3").html("");
    });
}

$(function () {
    $(document).on("click", ".fieldList", function () {
        reloadInfo();
    });
    //点击全选
    $(document).on("click", "#fieldAll", function () {
        var checked = this.checked;
        $(".fieldList").each(function () {
            this.checked = checked;
        });
        reloadInfo();
    });

    //别名文本框
    $("#table_as_name").keyup(function () {
        var filedArr = new Array();
        var index = 0;
        $(".fieldList").each(function (i) {
            if (this.checked) {
                filedArr[index] = this.name;
                index++;
                //bgclor
                $(".p" + this.name).addClass("bgclor");
            } else {
                $(".p" + this.name).removeClass("bgclor");
            }
        });
        reloadSqlInfo(filedArr);
    });

});

//刷新另外两个页面
function reloadInfo() {

    var filedArr = new Array();
    var index = 0;
    $(".fieldList").each(function (i) {
        if (this.checked) {
            filedArr[index] = this.name;
            index++;
            //bgclor
            $(".p" + this.name).addClass("bgclor");
        } else {
            $(".p" + this.name).removeClass("bgclor");
        }
    });
    reloadSqlInfo(filedArr);
    reloadDocInfo(filedArr);
}

//刷新查询sql 页面
function reloadSqlInfo(filedArr) {
    var as = $("#table_as_name").val();
    var all = "";
    var allas = "";
    if (as.length > 0) {
        as = as + ".";
    } else {
        as = "";
    }
    for (var i = 0; i < filedArr.length; i++) {
        all += as + tableMap[filedArr[i]].name + " , ";
        allas += as + tableMap[filedArr[i]].name + " " + tableMap[filedArr[i]].javaName + " , ";
    }
    all = all.substring(0, all.length - 2);
    allas = allas.substring(0, allas.length - 2);

    $("#liInfo_div1").html(all);
    $("#liInfo_div2").html(allas);
}

//刷新doc 页面
function reloadDocInfo(filedArr) {
    var all = "";
    for (var i = 0; i < filedArr.length; i++) {
        all += tableMap[filedArr[i]].javaName + " : " + tableMap[filedArr[i]].comment + "<br/>";
    }
    $("#liInfo_div3").html(all);
}
